package com.anda.logistics.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.anda.logistics.model.DriverLocation;
import com.anda.logistics.service.DriverLocationService;
import com.anda.logistics.util.CommonUtil;
import com.anda.logistics.util.LocationInfo;
import com.anda.logistics.vo.JsonReturnVo;
import com.fasterxml.jackson.databind.ObjectMapper;

//@Component("RecieveDriverLocationAction")
//@Scope("prototype")
@ParentPackage("json-default")
public class RecieveDriverLocationAction extends AndaBaseAction {

	private static final long serialVersionUID = -4551336268234044145L;
	private String l;
	private String v;
	private String n;
	private DriverLocationService driverLocationService;
//	private TruckInfoService truckInfoService;
	private JsonReturnVo jsonRet;
	
	@Action(value="/upload/location"
		,results=@Result(name="success",type="json",params={"root","jsonRet","contentType","text/html","enableGZIP","true","noCache","true"}))
	public String execute(){
		jsonRet = new JsonReturnVo("OK");
		try {
			ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
			LocationInfo locationInfo = null;
			if (v != null && v.equals("1")){
				locationInfo = mapper.readValue ( decodeUrlStr(l, true), LocationInfo.class);
			}
			
			if (locationInfo != null){
				Date d = new Date();
				n = n.trim();
				if (n.length() > 11){
					n = n.substring(n.length() - 11);
				}
				DriverLocation loc = new DriverLocation(n, locationInfo.getLng(), locationInfo.getLat() );
				d.setTime(locationInfo.getGetTime());
				loc.setGetTime(d);
				loc.setType(Integer.valueOf(locationInfo.getType()));
				loc.setUpTime(new Date());
				loc.setAccessToken(locationInfo.getAccessToken());
				loc.setAccuracy(locationInfo.getAccuracy());
				loc.setCity(locationInfo.getCity());
				loc.setCountry(locationInfo.getCountry());
				loc.setCountryCode(locationInfo.getCountryCode());
				loc.setRegion(locationInfo.getRegion());
				loc.setStreet(locationInfo.getStreet());
				loc.setStreetNumber(locationInfo.getStreetNumber());

				loc.setAddressDetail( locationInfo.getAddressDetail());
				loc.setCityCode(locationInfo.getCityCode());
				loc.setFeatureName(locationInfo.getFeatureName());
				loc.setLocationDesc(locationInfo.getLocationDesc());
				loc.setSubAdminArea( locationInfo.getSubAdminArea());
				loc.setSubLocality(locationInfo.getSubLocality());
				loc.setThoroughfare(locationInfo.getThoroughfare());
				
				System.out.println(l);
				System.out.println(loc);
				driverLocationService.save(loc);
				System.out.println(loc);
			} else {
				jsonRet.setStatus("NONE");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			jsonRet.setMsg(CommonUtil.escapeUnicode(e.getMessage())) ;
			jsonRet.setStatus("ERROR");
		} 
		return SUCCESS;
	}
	
	@Action(value="/upload/test"
			,results=@Result(name="success",type="json",params={"root","jsonRet","contentType","text/html","enableGZIP","true","noCache","true"}))
	public String uploadTest(){
		jsonRet = new JsonReturnVo("OK");
		return SUCCESS;
	}
	
	
	private String decodeUrlStr(String s, boolean decode){
		if (s.equals("null")) return null;
		try {
			if (decode) return URLDecoder.decode(s, "utf-8");
			return s;
		} catch (Exception e){
			return null;
		}
	}
	
	public void setL(String l) {
		this.l = l;
	}
	
	@Resource
	public void setDriverLocationService(DriverLocationService driverLocationService) {
		this.driverLocationService = driverLocationService;
	}
//	@Resource
//	public void setTruckInfoService(TruckInfoService truckInfoService){
//		this.truckInfoService = truckInfoService;
//	}
	
	public static void main(String [] args) throws UnsupportedEncodingException{
//		String s = "25.06453333333333,102.72085, 8613887012875,1343914638099,1,0.0,null,null,null,null,null,null,null,";
//		String [] info = s.split(",");
//		if (info[2].trim().length() == 13 && info[2].trim().startsWith("86")){
//			info[2] = info[2].trim().substring(2);
//		}
//		for(int i=0; i<info.length; i++){
//			System.out.print(i+1);
//			System.out.print(" : ");
//			//System.out.println(decodeUrlStr(info[i],true));
//		}
		String n = " 8613887012875";
		n = n.trim();
		if (n.length() > 11){
			n = n.substring(n.length() - 11);
		}
		System.out.println(n);
	}

	public void setV(String v) {
		this.v = v;
	}
	public void setN(String n){
		this.n = n;
	}

	public JsonReturnVo getJsonRet() {
		return jsonRet;
	}

	public void setJsonRet(JsonReturnVo jsonRet) {
		this.jsonRet = jsonRet;
	}
}
